package com.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shop.bean.Admin;
import com.shop.service.LoginService;
import com.shop.service.impl.LoginServiceImpl;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/login.do")
public class LoginServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		LoginService ls = new LoginServiceImpl();
		String account = req.getParameter("account");
		String password = req.getParameter("password");
		Admin admin = new Admin();
		if(ls.getAdminByAP(account, password) == null) {
			if(ls.getAdminByAccount(account) != null) {
				req.setAttribute("msg", "密码错误");
			}else {
				req.setAttribute("msg", "用户不存在");
			}
			req.getRequestDispatcher("Login.jsp").forward(req, resp);
		}else {
			admin.setAccount(account);
			admin.setPassword(password);
			session.setAttribute("admin", admin);
			resp.sendRedirect("goods.do");
		}
	}
}
